package com.rocpenghua.excel.easyexcel;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.rocpenghua.util.JSONUtils;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengp
 * @date 2024年02月27日 14:12
 */
public class EasyDataUtil {
    public static List<List<String>> getExcelHead(String tableName,String tableTitle){
        System.out.println(tableTitle);
        System.out.println(tableName);
        /**
         * [
         * {
         *     code:"1",
         *     name:"1",
         *     children:[
         *     {
         *         code:"1",
         *         name:"1“
         *     }]
         * }
         * ]
         */
        List<Map> titleList = JSONUtils.jsonToList(tableTitle,Map.class);
        List<List<String>> headList = new ArrayList<>();
        for(int i=0;i<titleList.size();i++){

            Map map = titleList.get(i);
            String head1 = (String) map.get("name");
            if(map.get("children") != null){
                List<Map> children = map.get("children") instanceof String ?
                        JSONUtils.jsonToList((String) map.get("children"),Map.class): (List<Map>) map.get("children");
                for(int j=0;j<children.size();j++){
                    Map child = children.get(j);
                    List<String> list = new ArrayList<>();
                    list.add(tableName);
                    list.add(head1);
                    list.add((String) child.get("name"));
                    headList.add(list);
                }
            }else{
                List<String> list = new ArrayList<>();
                list.add(tableName);
                list.add(head1);
                headList.add(list);
            }
        }
        return headList;
    }
    public static List<String> getTitleCode(String tableTitle){
        /**
         * [
         * {
         *     code:"1",
         *     name:"1",
         *     children:[
         *     {
         *         code:"1",
         *         name:"1“
         *     }]
         * }
         * ]
         */
        List<Map> titleList = JSONUtils.jsonToList(tableTitle,Map.class);
        List<String> titleCodeList = new ArrayList<>();
        for(int i=0;i<titleList.size();i++){
            List<String> list = new ArrayList<>();
            Map map = titleList.get(i);
            if(map.get("children") != null){
                List<Map> children = map.get("children") instanceof String ?
                        JSONUtils.jsonToList((String) map.get("children"),Map.class): (List<Map>) map.get("children");
                for(int j=0;j<children.size();j++){
                    Map child = children.get(j);
                    titleCodeList.add((String) child.get("code"));
                }
            }else{
                titleCodeList.add((String) map.get("code"));
            }
        }
        return titleCodeList;
    }
    public static List<List<String>> getExcelData(String tableTitle,String tableData){
        final List<String> titleCode = getTitleCode(tableTitle);
        List<List<String>> dataList = new ArrayList<>();
        if(tableData != null){
            List<Map> data = JSONUtils.jsonToList(tableData,Map.class);
            for(int i=0;i<data.size();i++){
                Map map = data.get(i);
                List<String> list = new ArrayList<>();
                for(int j=0;j<titleCode.size();j++){
                    list.add(JSONUtils.getMapStrValueByKey(map,titleCode.get(j)));
                }
                dataList.add(list);
            }
        }
        return dataList;
    }
    public static Map getParamMap(String tableParam){
        Map paramMap = new HashMap<>();
        if (tableParam.contains("&") && tableParam.contains("=")) {
            String[] params = tableParam.split("&");
            for (int i = 0; i < params.length; i++) {
                if (params[i].split("=").length > 1) {
                    paramMap.put(params[i].split("=")[0], params[i].split("=")[1]);
                } else {
                    paramMap.put(params[i].split("=")[0], "");
                }
            }
            tableParam = JSONUtils.mapToJson(paramMap);
        }
        return JSONUtils.jsonToObject(tableParam, Map.class);
    }

    /**
     * 设置excel样式
     */
    public static HorizontalCellStyleStrategy getStyleStrategy() {
        // 头的策略  样式调整
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 头背景 浅绿
        WriteFont headWriteFont = new WriteFont();
        // 头字号
        headWriteFont.setFontHeightInPoints((short) 13);
        // 字体样式
        headWriteFont.setFontName("宋体");
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 自动换行
        headWriteCellStyle.setWrapped(true);
        // 设置细边框
        headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        headWriteCellStyle.setBorderRight(BorderStyle.THIN);
        headWriteCellStyle.setBorderTop(BorderStyle.THIN);
        // 水平对齐方式
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 垂直对齐方式
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 内容的策略 宋体
        WriteCellStyle contentStyle = new WriteCellStyle();
        // 设置垂直居中
        contentStyle.setWrapped(true);
        // 水平对齐方式
        contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 垂直对齐方式
        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 水平居中
//        contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        WriteFont contentWriteFont = new WriteFont();
        // 内容字号
        contentWriteFont.setFontHeightInPoints((short) 12);
        // 字体样式
        contentWriteFont.setFontName("宋体");
        contentStyle.setWriteFont(contentWriteFont);
        contentStyle.setBorderBottom(BorderStyle.THIN);
        contentStyle.setBorderLeft(BorderStyle.THIN);
        contentStyle.setBorderRight(BorderStyle.THIN);
        contentStyle.setBorderTop(BorderStyle.THIN);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentStyle);
    }
}
